//默认lodash没有被打包是因为它使用了commonjs,rollup默认情况下只会处理es module
const commonjs = require("@rollup/plugin-commonjs");

const nodeResolve = require("@rollup/plugin-node-resolve");

//es6转换es5
const { babel } = require("@rollup/plugin-babel");

//压缩js代码
const terser = require("@rollup/plugin-terser");

//css
const postcss = require("rollup-plugin-postcss");

//vue
const vue = require("rollup-plugin-vue");

//解决vue 报错process
const replace = require("@rollup/plugin-replace");

//开启本地服务
const serve = require("rollup-plugin-serve");

//开启自动刷新浏览器
const livereload = require("rollup-plugin-livereload");

//区分环境 脚本配置
console.log(process.env.NODE_ENV);

const isProduction = process.env.NODE_ENV === "production";

let plugins = [
  commonjs(), //解决以cmj方式导出的
  nodeResolve(), //解决从node_module引入的第三方包无法打包
  babel({
    babelHelpers: "bundled",
    exclude: /node_modules/,
    presets: ["@babel/preset-env"],
  }),

  postcss({ plugins: [require("postcss-preset-env")] }),
  vue(),
  replace({
    "process.env.NODE_ENV": JSON.stringify("production"),
    preventAssignment: true,
  }),
];

//区分环境
if (isProduction) {
  plugins.push(terser());
} else {
  const exactPlugin = [
    serve({
      open: true,
      contentBase: ".",
      port: 8000,
    }),
    livereload(),
  ];
  plugins.push(...exactPlugin);
}

module.exports = {
  //入口
  input: "./src/main.js",
  //出口
  output: [
    {
      format: "iife",
      name: "myUtils",
      file: "./build/bundle.js",
    },
  ],
  plugins: plugins,
  external: ["lodash"],
};
